package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.MenuDto;
import com.itheima.ydd.dto.MenuDto2;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys_menu")
@Slf4j
public class MenuController {
    @Autowired
    private MenuService menuService;

    @PostMapping("/update")
    public ResultData update(@RequestBody Menu menu){
        menuService.updateById(menu);
        return ResultData.ok("修改成功",menu);
    }

    @GetMapping("/del")
    public ResultData delete(Integer id){
        LambdaQueryWrapper<Menu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Menu::getPid,id);
        int count = menuService.count(lqw);
        if (count>0){
            throw new BusinessException("该目录存在子目录");
        }
        menuService.removeById(id);
        return ResultData.ok("删除成功",id);
    }

    @GetMapping("allData")
    public ResultData allData(){

        LambdaQueryWrapper<Menu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getPid,0);
        List<Menu> list = menuService.list(queryWrapper);

        List<MenuDto2> oneTwoThree=list.stream().map((item->{
            MenuDto2 menuDto=new MenuDto2();
            BeanUtils.copyProperties(item,menuDto);

            LambdaQueryWrapper<Menu> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(Menu::getPid,item.getId());
            List<Menu> list1 = menuService.list(queryWrapper1);

            List<MenuDto2> twoThree=list1.stream().map((item2->{
                MenuDto2 menuDto1=new MenuDto2();
                BeanUtils.copyProperties(item2,menuDto1);

                LambdaQueryWrapper<Menu> queryWrapper2=new LambdaQueryWrapper<>();
                queryWrapper2.eq(Menu::getPid,item2.getId());
                List<Menu> list2 = menuService.list(queryWrapper2);

                menuDto1.setChildren(list2);
                return menuDto1;
            })).collect(Collectors.toList());

            menuDto.setChildren(twoThree);
            return menuDto;
        })).collect(Collectors.toList());

        //List<MenuDto> menuDtoList=menuService.allData();
        return ResultData.ok("查询成功",oneTwoThree);
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody Menu menu){
        menu.setCreate_date(LocalDateTime.now());
        menuService.save(menu);
        return ResultData.ok("添加成功",menu);
    }

    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id){
        Menu menu = menuService.getById(id);
        return ResultData.ok("查询成功",menu);
    }
}
